package com.example.module.ai.controller;

import com.example.framework.common.pojo.R;
import com.example.module.ai.event.NotifyEvent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@Tag(name = "消息推送")
@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Operation(summary = "给指定id的websocket连接发送通知")
    @GetMapping("/notify")
    // 编译时没有保留方法参数名，导致无法确定参数名，会报错“Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.”
    // 添加插件解决
    public R<String> save(@Parameter(description = "id类型，1：sessionId，2：userId") @NotNull @RequestParam Integer idType,
                          @NotEmpty @RequestParam String id,
                          @NotEmpty @RequestParam String msg) {
        applicationEventPublisher.publishEvent(new NotifyEvent(this, idType, id, msg));
        return R.success("推送成功");
    }

}